home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3245 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.6 KB  |  81 lines

  1. Path: newsfeed.acns.nwu.edu!ftpbox!mothost!news
  2. From: bhowes@cssun3.corp.mot.com (Brad Howes)
  3. Newsgroups: comp.lang.c++
  4. Subject: Type Converter Problem
  5. Date: 22 Jan 1996 23:34:20 GMT
  6. Organization: Motorola, Inc.
  7. Distribution: world
  8. Message-ID: <BHOWES.96Jan22163420@cssun3.corp.mot.com>
  9. Reply-To: bhowes@cssun3.corp.mot.com
  10. NNTP-Posting-Host: cssun3.corp.mot.com
  11.  
  12. After running the code below, I get A::active == -1, and B::active == 0. Anyone
  13. with an explaination why? Or is my compiler bugged...
  14.  
  15.   class A {
  16.     long a;
  17.   
  18.   public:
  19.     static long active; // keep track of all A objects in existence
  20.     A( int x = 0 ) : a( x ) { ++active; };
  21.     ~A() { --active; };
  22.   }
  23.   
  24.   class B {
  25.     char b[ 4 ];
  26.   
  27.   public:
  28.     static long active; // keep track of all B objects in existence
  29.     B( char *x = "\0\0\0\0" ) { memcpy( b, x, 4 ); ++active; };
  30.     ~B() { --active; };
  31.     //
  32.     // Converter to A class object
  33.     //
  34.     operator A() { return A(((b[0]*256+buf[1])*256+buf[2])*256+buf[3]); };
  35.   }
  36.  
  37.   long A::active = 0;
  38.   long B::active = 0;
  39.  
  40.   static void
  41.   f( void ) {
  42.     B b( "FOOL" ); // calls B::B()
  43.     A a( b ); // calls B::operator A() and then A::A(
  44.     //
  45.     // ??? Should be 1 and 1, right???
  46.     //
  47.     cout << "number of B objects: " << B::active << '\n';
  48.     cout << "number of A objects: " << A::active << '\n';
  49.   }
  50.   
  51.   
  52.   int
  53.   main( int argc, char *argv[] )
  54.   {
  55.     f();
  56.     //
  57.     // ??? Should be 0 and 0. I get 0 and -1.
  58.     //
  59.     cout << "number of B objects: " << B::active << '\n';
  60.     cout << "number of A objects: " << A::active << '\n';
  61.   }
  62.  
  63. Thanks,
  64.  
  65. Brad
  66. +---------------------------------------------------------------------------+
  67. | Brad Howes                       | Motorola E-Mail: XBH001                |
  68. | EMT Development                  | Internet: bhowes@cssun3.corp.mot.com   |
  69. | Motorola Corporate - MD H1780    |           xbh001@email.mot.com         |
  70. | 8111 E. McDowell Rd.             | Voice: 602 441 1522  Fax: 602 441 5455 |
  71. | Scottsdale, AZ  85257-3806       | Pager: 602 519 4227                    |
  72. +---------------------------------------------------------------------------+
  73. -- 
  74. +---------------------------------------------------------------------------+
  75. | Brad Howes                       | Motorola E-Mail: XBH001                |
  76. | EMT Development                  | Internet: bhowes@cssun3.corp.mot.com   |
  77. | Motorola Corporate - MD H1780    |           xbh001@email.mot.com         |
  78. | 8111 E. McDowell Rd.             | Voice: 602 441 1522  Fax: 602 441 5455 |
  79. | Scottsdale, AZ  85257-3806       | Pager: 602 519 4227                    |
  80. +---------------------------------------------------------------------------+
  81.